﻿using System.Collections.ObjectModel;
using System.ComponentModel;
namespace Apps.Yunwei.ViewModels
{
    public class TopicItemObject : INotifyPropertyChanged
    {
        public TopicItemObject()
        {
            this.Children = new ObservableCollection<TopicItemObject>();
            this.Type = TopicItemType.Directory;
        }
        private string CaptionField;
        private string IDField;
        private string ImageUrl1Field;
        private string BackgroundField;
        private string OrderField;
        private bool IsCheckedField=false;
        private bool IsExpandedField=false;
        public TopicItemObject Parent { get; set; }

        public ObservableCollection<TopicItemObject> Children { get; set; }
        public TopicItemType Type { get; set; }
        public string Tags { get; set; }
        public string Caption
        {
            get
            {
                return this.CaptionField;
            }
            set
            {
                if ((object.ReferenceEquals(this.CaptionField, value) != true))
                {
                    this.CaptionField = value;
                    this.Notify("Caption");
                }
            }
        }
        public string ID
        {
            get
            {
                return this.IDField;
            }
            set
            {
                if ((object.ReferenceEquals(this.IDField, value) != true))
                {
                    this.IDField = value;
                    this.Notify("ID");
                }
            }
        }
        public string ImageUrl1
        {
            get
            {
                return this.ImageUrl1Field;
            }
            set
            {
                if ((object.ReferenceEquals(this.ImageUrl1Field, value) != true))
                {
                    this.ImageUrl1Field = value;
                    this.Notify("ImageUrl1");
                }
            }
        }
        public bool IsChecked
        {
            get
            {
                return this.IsCheckedField;
            }
            set
            {
                if ((this.IsCheckedField.Equals(value) != true))
                {
                    this.IsCheckedField = value;
                    this.OnCheckedChanged();
                    this.Notify("IsChecked");
                }
            }
        }
        public bool IsExpanded
        {
            get
            {
                return this.IsExpandedField;
            }
            set
            {
                if ((this.IsExpandedField.Equals(value) != true))
                {
                    this.IsCheckedField = value;
                    this.OnExpandedChanged();
                    this.Notify("IsExpanded");
                }
            }
        }
        public string Background
        {
            get
            {
                return this.BackgroundField;
            }
            set
            {
                if ((object.ReferenceEquals(this.BackgroundField, value) != true))
                {
                    this.BackgroundField = value;
                    this.Notify("Background");
                }
            }
        }
        public string Order
        {
            get
            {
                return this.OrderField;
            }
            set
            {
                if ((object.ReferenceEquals(this.OrderField, value) != true))
                {
                    this.OrderField = value;
                    this.Notify("Order");
                }
            }
        }

        private void OnCheckedChanged()
        {
            foreach (var item in this.Children)
            {
                item.IsChecked = this.IsChecked;
            }
        }
        private void OnExpandedChanged()
        {
            foreach (var item in this.Children)
            {
                item.IsExpanded = this.IsExpanded;
            }
        }

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;

        private void Notify(string info)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(info));
            }
        }
        #endregion
    }

    public enum TopicItemType
    {
        Directory=0,
        App=1,
        Service=2
    }
}
